package com.itzsj.controller;

import com.itzsj.common.utils.JWTUtil;
import com.itzsj.common.properties.JwtProperties;
import com.itzsj.common.result.Result;
import com.itzsj.pojo.dto.LoginDTO;
import com.itzsj.pojo.dto.RegisterDTO;
import com.itzsj.service.CommonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/start")
public class CommonController {

    @Resource
    private CommonService commonService;

    @Resource(name = "jwtProperties", type = JwtProperties.class)
    private JwtProperties jwtProperties;


    @PostMapping("/userLogin")
    public Result login(@RequestBody LoginDTO loginDTO) {
        log.info("loginDTO {}", loginDTO);
        commonService.login(loginDTO);
        Map<String, Object> claims = new HashMap<>();
        claims.put("ACCOUNT", loginDTO.getAccount());
        claims.put("PASSWORD", loginDTO.getPassword());
        String jwtToken = JWTUtil.createJWT(
                claims, jwtProperties.getSecretKey(), jwtProperties.getTtMillis());
        log.info("jwt token {}", jwtToken);
        return Result.success(jwtToken);

    }

    @PostMapping("/userRegister")
    public Result register(@RequestBody RegisterDTO registerDTO) {
        log.info("registerDTO {}", registerDTO);
            commonService.register(registerDTO);
        return Result.success();
    }

}
